package inits

import (
	"api-gateway/basic/config"
	proto "api-gateway/basic/proto"
	"api-gateway/utils"
	"flag"
	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"
	"log"
)

// 初始化用户服务的grpc连接
func InitUserRpc() {

	userCfg := utils.ServiceConfig{
		Name:    "user-service",
		Tags:    []string{"user", "user-service", "user-srv"},
		Address: "127.0.0.1",
		Port:    50052,
	}

	utils.NewConsul("127.0.0.1:8500").Register(userCfg)

	flag.Parse()
	// Set up a connection to the server.
	conn, err := grpc.NewClient("127.0.0.1:50052", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Fatalf("did not connect: %v", err)
	}
	//c := pb.NewGreeterClient(conn)
	config.UserClient = proto.NewUserClient(conn)

}

func InitPatientRpc() {
	flag.Parse()
	// Set up a connection to the server.
	conn, err := grpc.NewClient("127.0.0.1:50054", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Fatalf("did not connect: %v", err)
	}
	//c := pb.NewGreeterClient(conn)
	config.PatientClient = proto.NewPatientServiceClient(conn)

}

// 初始化医生服务的grpc连接
func InitDoctorRpc() {
	flag.Parse()
	// Set up a connection to the server.
	conn, err := grpc.NewClient("127.0.0.1:50053", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Fatalf("did not connect to doctor service: %v", err)
	}

	// 创建医生相关服务的客户端
	config.DoctorClient = proto.NewDoctorServiceClient(conn)
	config.ConsultationClient = proto.NewConsultationClient(conn)
	config.MedicineClient = proto.NewMedicineClient(conn)
	config.MedicalRecordClient = proto.NewMedicalRecordClient(conn)
	config.AttachmentClient = proto.NewAttachmentClient(conn)
	config.StatisticsClient = proto.NewStatisticsClient(conn)

	log.Println("Doctor service gRPC client initialized")
}
